package com.kobeliu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Collections2;
import com.kobeliu.base.BaseInfoProperties;
import com.kobeliu.mapper.JobTypeMapper;
import com.kobeliu.pojo.JobType;
import com.kobeliu.pojo.vo.JobTypeSecondAndThirdVO;
import com.kobeliu.pojo.vo.JobTypeThirdVO;
import com.kobeliu.service.JobTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
@Slf4j
public class JobTypeServiceImpl extends BaseInfoProperties implements JobTypeService {

    @Autowired
    private JobTypeMapper jobTypeMapper;

    @Override
    public List<JobType> initTopLIst() {

        return jobTypeMapper.selectList(new QueryWrapper<JobType>()
                .eq("is_leaf", false)
                .eq("father_id", "0")
                .eq("level", 1));
    }

    @Override
    public List<JobTypeSecondAndThirdVO> getSecondAndThirdListByTop(String fatherId) {

        List<JobTypeSecondAndThirdVO> types = new ArrayList<>();

        List<JobType> jobTypes = jobTypeMapper.selectList(new QueryWrapper<JobType>()
                .eq("father_id", fatherId)
                .eq("level", 2)
                .orderByAsc("sort"));

        for (JobType jt:jobTypes){
            types.add(getThirdJobType(jt));
        }

        return types;
    }

    private JobTypeSecondAndThirdVO getThirdJobType(JobType jobType) {

        JobTypeSecondAndThirdVO type = new JobTypeSecondAndThirdVO();

        type.setSecondLevelId(jobType.getId());
        type.setSecondLevelName(jobType.getName());
        type.setSecondLevelSort(jobType.getSort());

        List<JobType> jobTypes = jobTypeMapper.selectList(new QueryWrapper<JobType>()
                .eq("father_id", jobType.getId())
                .eq("level", 3)
                .orderByAsc("sort"));

        List<JobTypeThirdVO> jobTypeThirdVOList = jobTypes.stream().map(obj -> {
            JobTypeThirdVO vo = new JobTypeThirdVO();
            vo.setThirdLevelId(obj.getId());
            vo.setThirdLevelName(obj.getName());
            vo.setThirdLevelSort(obj.getSort());
            return vo;
        }).collect(Collectors.toList());

        type.setJobTypeThirdVOList(jobTypeThirdVOList);

        return type;
    }
}
